<?php
namespace app\http\controller;
use think\Controller;
use think\Db;
use app\service\hzcityService;

class Sub extends Controller
{
    /**
     * 要使用命令行运行此方法！！！   
     *  think5.0 运行方法为 cd到Public 目录  然后  php index.php 模块/控制器/方法
     * 该类主要为订阅，建议订阅代码和发布代码不要写在同一个类中，避免修改造成不必要的误改。
     * 每次更新该类后需要重启mqtt订阅，否则新的改动不会生效。
     * 请在相应的位置放入phpMQTT的库
     * 库代码：https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php
     * 类库使用的时候注意命名空间，类名称命名要和thinkphp的保持一致，不然会报错
     */
    public function sub(){
        
        $server = "parking.hzgxtc.com";     // 服务代理地址(mqtt服务端地址)
        $port = 1883;                     // 通信端口
        $username = "A20190619152105";                 // 用户名(如果需要)
        $password = "TTTC_2020_ZaoZao";                   // 密码(如果需要M00079
        $client_id = "A20190619152105"; //你的连接客户端id
 
        $mqtt = new Mqtt($server, $port, $client_id);    //进行连接
 
        if(!$mqtt->connect(true, NULL, $username, $password)) {
            exit('error');   //连接失败
        } else {
            $path='/www/wwwroot/parkingcommunity/public/log/hzcity/'.date('Ymd');
            if (! file_exists ( $path )) {
                mkdir ( "$path", 0777, true );
            }
            file_put_contents($path."/mqtt_login.txt","时间: " . date("Y-m-d H:i:s") . "\n",FILE_APPEND);
            echo "success"; //连接成功
        }
        $topics['/hzcity/v2/departurePayInfo/A20190619152105/#'] = array("qos" => 0, "function" => array($this,"procmsg"));
        $topics['/hzcity/v2/fee/A20190619152105/#'] = array("qos" => 0, "function" => array($this,"fee"));
        $topics['/hzcity/v2/payResult/A20190619152105/#'] = array("qos" => 0, "function" => array($this,"payResult"));
        //var_dump($topics);
        // 订阅主题为 xxx3809293670ctr qos为0
        $mqtt->subscribe($topics, 0);
         
        while($mqtt->proc()){
            
        }
        //死循环监听
        $c->loopForever();
    }
    function procmsg($topic, $msg){ //信息回调函数 打印信息
        if(!empty($msg)){
            $path='/www/wwwroot/parkingcommunity/public/log/hzcity/'.date('Ymd');
            if (! file_exists ( $path )) {
                mkdir ( "$path", 0777, true );
            }
            echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
            echo "Topic: {$topic}\n";
            //echo "Msg:{$msg}\n";
            
            
            
            $data=json_decode($msg,true);
            
            $carinfo=(new hzcityService())->pubkeyDecrypt($data["cipher"]);
            echo "requestId:".$data["requestId"]."\n";
            echo "Carinfo:{$carinfo}\n";
            $carinfolog=$carinfo;
            
            //var_dump($data);
            $carinfo=json_decode($carinfo,true);
            $logname=$path."/departurePayInfo_".$carinfo["parkingCode"].".txt";
            file_put_contents($logname,"Msg Recieved: " . date("Y-m-d H:i:s") . "\n",FILE_APPEND);
            file_put_contents($logname,"Topic: ".$topic."\n",FILE_APPEND);
            file_put_contents($logname,"requestId: ".$data["requestId"]."\n",FILE_APPEND);
            file_put_contents($logname,"carinfo: ".$carinfolog."\n",FILE_APPEND);
            
            $carinfo["requestId"]=$data["requestId"];
            $res2=(new hzcityService())->confirmDeparturePayInfo($carinfo);
            file_put_contents($logname,"confirmDeparturePayInfo返回: ".$res2."\n\n",FILE_APPEND);
            echo $res2."\n\n\n";
        }  
    }
    
    function fee($topic, $msg){ //信息回调函数 打印信息
        if(!empty($msg)){
            echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
            echo "Topic: {$topic}\n";
            echo "Msg:{$msg}\n";
        }  
    }
    
    function payResult($topic, $msg){ //信息回调函数 打印信息
        if(!empty($msg)){
            $path='/www/wwwroot/parkingcommunity/public/log/hzcity/'.date('Ymd');
            if (! file_exists ( $path )) {
                mkdir ( "$path", 0777, true );
            }
            echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
            echo "Topic: {$topic}\n";
            //echo "Msg:{$msg}\n";
            
            
           
            $data=json_decode($msg,true);
            
            $payinfo=(new hzcityService())->pubkeyDecrypt($data["cipher"]);
            echo "requestId:".$data["requestId"]."\n";
            echo "payinfo:{$payinfo}\n\n";
            $payinfolog=$payinfo;
            
            //var_dump($data);
            $payinfo=json_decode($payinfo,true);
            
            $logname=$path."/applyPay_".$payinfo["parkingCode"].".txt";
            $payinfo["requestId"]=$data["requestId"];
            file_put_contents($logname,"Msg Recieved: " . date("Y-m-d H:i:s") . "\n",FILE_APPEND);
            file_put_contents($logname,"Topic: ".$topic."\n",FILE_APPEND);
            file_put_contents($logname,"requestId: ".$data["requestId"]."\n",FILE_APPEND);
            file_put_contents($logname,"payinfo: ".$payinfolog."\n\n",FILE_APPEND);
            $res2=(new hzcityService())->confirmPayResult($payinfo);
            file_put_contents($logname,"res2: ".$res2."\n\n",FILE_APPEND);
            echo $res2."\n\n\n";
        }  
    }
}